package com.woniuxy.rbac;

import com.woniuxy.rbac.entity.Perm;
import com.woniuxy.rbac.service.PermService;
import com.woniuxy.rbac.shiro.DbRealm;
import com.woniuxy.rbac.shiro.JwtFilter;
import com.woniuxy.rbac.shiro.SysRealm;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: L
 */
@Configuration
public class ShiroConfig {
    @Bean
    public Realm realm() {
        return new DbRealm();
    }



    @Bean
    public SysRealm sysRealm() {
        HashedCredentialsMatcher credentialsMatcher=new HashedCredentialsMatcher();
        credentialsMatcher.setHashIterations(1024);
        credentialsMatcher.setHashAlgorithmName("md5");
        SysRealm sysRealm=new SysRealm();
        sysRealm.setCredentialsMatcher(credentialsMatcher);
        return sysRealm;
    }

    @Bean
    public CacheManager cacheManager(){
        EhCacheManager cacheManager = new EhCacheManager();

        return null;
    }

    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager=new DefaultWebSecurityManager();
      //  defaultWebSecurityManager.setRealm(realm());
        ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
        authenticator.setRealms(Arrays.asList(realm(),sysRealm()));
        defaultWebSecurityManager.setAuthenticator(authenticator);
        return defaultWebSecurityManager;
    }

    @Autowired
    private PermService permService;
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        List<Perm> perms = permService.getPerms();
//        for (Perm perm:perms) {
//            chainDefinition.addPathDefinition(perm.getLink(),"authc,perms["+perm.getCode()+"]");
//        }
        chainDefinition.addPathDefinition("/**","anon");
        return chainDefinition;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setLoginUrl("/member/login");
        shiroFilterFactoryBean.setUnauthorizedUrl("/member/login");
        shiroFilterFactoryBean.setSecurityManager(securityManager());

        Map<String, Filter> filters=new HashedMap();
        filters.put("jwt",new JwtFilter());
        shiroFilterFactoryBean.setFilters(filters);

        Map<String,String> map=new LinkedHashMap<>();
        map.put("/api/v1/user/login","anon");
        map.put("/ucenter/**","authc");
        map.put("/order/add","authc");
        map.put("/member/logout","logout");
        map.put("/api/v1/**","jwt,perms[subject_all]");

        map.put("/**","anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        return shiroFilterFactoryBean;
    }
}
